/*
 * @Author: your name
 * @Date: 2022-02-21 08:39:57
 * @LastEditTime: 2022-02-21 14:33:50
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /zxd/dome_vue/src/router/index.js
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import Cookies from 'js-cookie';
import store from '../store'
// 基础路由
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// 设置白名单
const whiteList = ['/login'];

router.beforeEach((to, from, next) => {
  let token = Cookies.get('token');
  let author = Cookies.get('author');
  let routerList = store.state.routerList;
  if(whiteList.includes(to.path)) {
    next();
  } else {
    // 判断一下 token 存在否
    if(token) {
      // 是不是退出状态
      if(routerList.length <= 0 && author) {
        store.dispatch('ROUTER_LIST', author);
        next({ ...to, replace: true })
      } else {
        next();
      }
    } else {
      next('/login')
    }
  }
})

export default router
